
#include "Synch.h"
#include <stdio.h>
#include <iostream>

using namespace std;

Synch::Synch() {
    m_pauseThreads = true;
    m_sleepThreads = false;
    m_terminateThreads = false;
}

void Synch::startAgent(Thread *thread) {
    Lock();
    m_threads.push_back(thread);
    printf("StartAgent: current number of threads: %d\n", (int) m_threads.size());
    Unlock();
}//end startAgent

void Synch::resumeThreads() {
    Lock();
    if (m_pauseThreads == true) {
        m_pauseThreads = false;
        Unlock();
        for (int i = 0; i < m_threads.size(); i++) {
            if (m_threads[i] == NULL) {
                printf("WakeUpThreads: m_threads[%d] is NULL!\n", i);
            } else {
                m_threads[i]->Lock();
                m_threads[i]->notify();
                m_threads[i]->Unlock();
            }//end else
        }//end for
    }
}//end resumeThreads

void Synch::terminateThreads() {
    if (m_pauseThreads == true) {
        resumeThreads();
    }
    m_terminateThreads = true;
}

std::vector<Thread *> Synch::m_threads;